﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Gestures;
//using GameSpace;
//using Microsoft.WPF.Framework.Graphics;
//using Microsoft.WPF.Framework;

namespace GesturePreviews.Gestures
{
    // class used to draw a gesture path on the screen and to keep track of coordinates and times of a gesture.
    public abstract class Gesture
    {
        // Range of points = [-0.5 to 0.5, -0.5 to 0.5]
        protected List<Common.Point> points;
        public List<DateTime> times;
        protected Common.Point lastPoint;
        protected DateTime lastTime;

        protected int id;

        // is finger motion finished?
        //protected bool gestureFinished = false;

        // can this gesture be deleted by the manager?
        //protected bool toBeDeleted = false;        


        public Gesture(int newID)
        {
            points = new List<Common.Point>();
            times = new List<DateTime>();
            id = newID;
        }

        public int getID()
        {
            return id;
        }

        //public abstract void onFinish();
        //public abstract void onStart();
        //public abstract void onDraw();
        //public abstract void onUpdate();

        //public double draw

        public List<Common.Point> getPoints()
        {
            return points;
        }



        //public double getMillisecondsSinceLastMovement()
        //{
        //TimeSpan ts = DateTime.Now.Subtract(lastTime);
        //return ts.TotalMilliseconds;
        //}

        public abstract void draw();

        public abstract void update();

        public virtual void addPoint(Common.Point p)
        {
            if (finished)
            {
                return;
            }

            if (lastPoint != null)
            {
                double dx = p.x - lastPoint.x;
                double dy = p.y - lastPoint.y;
                double distance = Math.Sqrt(dx * dx + dy * dy);

                if (distance < 8)
                {
                    return;
                }
            }

            /*
            if (lastPoint != null)
            {
                // TODO: betere oplossing voor detecteren "stilstand".
                double dx = p.x - lastPoint.x;
                double dy = p.y - lastPoint.y;
                double distance = Math.Sqrt(dx * dx + dy * dy);
                double velocity = distance / getMillisecondsSinceLastMovement();

                // TODO: deze snelheid configureerbaar maken.
                if (velocity < 0.00001)
                {
                    return;
                }
            }*/

            points.Add(p);
            lastPoint = p;
            times.Add(DateTime.Now);
            lastTime = DateTime.Now;

            //onUpdate();
        }

        public abstract void setFingerReleased();

        public abstract bool isFingerReleased();

        public abstract bool canBeDeleted();

        public TimeSpan getDuration()
        {
            return DateTime.Now.Subtract(lastTime);
        }


        bool paused;

        public bool isPaused()
        {
            if (points.Count < 2)
            {
                paused = true;
                return paused;
            }

            // TODO: betere oplossing voor detecteren "stilstand".
            double dx = points[points.Count - 1].x - points[points.Count - 2].x;
            double dy = points[points.Count - 1].y - points[points.Count - 2].y;

            double distance = Math.Sqrt(dx * dx + dy * dy);
            double dt = DateTime.Now.Subtract(times[times.Count - 2]).TotalSeconds;
            double velocity = distance / dt;

            // TODO: deze snelheid configureerbaar maken.
            if (velocity < 16)
            {
                paused = true;
                return paused;
            }

            paused = false;
            return paused;

        }

        public bool justPaused()
        {
            bool oldPaused = paused;
            isPaused();
            if (oldPaused == false && paused == true)
            {
                return true;
            }
            return false;
        }

        public bool clickMode = false;
        public List<String> detectedSoFar = new List<string>();

        // if finished, ignore all other points.
        public bool finished;
    }
}